package com.xiaohu.transfrom;

import com.xiaohu.bean.WaterSensor;
import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;


public class ReduceDemo {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        env.setParallelism(1);

        DataStreamSource<WaterSensor> senorDS = env.fromElements(
                new WaterSensor("s1", 1L, 5),
                new WaterSensor("s2", 2L, 2),
                new WaterSensor("s3", 3L, 3),
                new WaterSensor("s1", 4L, 4),
                new WaterSensor("s2", 5L, 5),
                new WaterSensor("s1", 6L, 6)
        );

        KeyedStream<WaterSensor, String> sensorKS = senorDS.keyBy(new KeySelector<WaterSensor, String>() {
            @Override
            public String getKey(WaterSensor value) throws Exception {
                return value.getId();
            }
        });


        //输入类型必须与输入类型一致！
        //每个分组第一条数据来的时候，不会执行reduce逻辑，而是存起来，当第二条以后来的时候，累计处理
        sensorKS.reduce(new ReduceFunction<WaterSensor>() {
            @Override
            public WaterSensor reduce(WaterSensor value1, WaterSensor value2) throws Exception {
                return new WaterSensor(value1.getId(),value2.getTs(),value1.getVc()+ value2.getVc());
            }
        }).print();

        env.execute();
    }
}
